<?php

/**
 * 商品竞拍管理
 * Enter description here ...
 * @author Admin
 *
 */
class AuctionAction extends CommonAction{

	
	/**
	 * 商品竞拍列表
	 * Enter description here ...
	 */
	public function index() {
		$goodsname =  $_REQUEST['goodsname'];      //检索条件
		$this->assign("goodsname",$goodsname);	
		
		$map['goodsname']= array('like',"%{$goodsname}%"); 
			
		$auction = D('Auction');  //竞拍

		
		import("@.ORG.Page"); //导入分页类
		
		$count = $auction->where($map)->count();    //计算总数
		
		$p = new Page ($count,12);
		
		
		$auctiondata=$auction
			->where($map)
			->limit($p->firstRow.','.$p->listRows)
			->order('id desc')    //创建时间降序排列
			->findAll();
			
			
		//...分页检索条件	
		$p->parameter = "goodsname=" . urlencode ( $_REQUEST['goodsname'] ) . "&";
		
		
		$page = $p->show();
		
		$this->assign("page",$page);
		$this->assign("auctiondata",$auctiondata);
		
		$this->display();
	}
	
	
	
	
	
	
	
	/**
	 * 添加竞拍
	 * @see CommonAction::add()
	 */
	public function add(){
		$gid = GetNum($_GET['id']);   //商品ID：通过商品列表中可直接添加该商品的竞拍
		$goods = D('Goods');
		if(!empty($gid)){
			Log::write("通过商品列表添加商品竞拍....",Log::DEBUG) ;
			
			$goodsdata = $goods->find($gid);    //获取商品信息
			$this->assign("goodsdata",$goodsdata);
			
			$this->display("goodsAdd");    //跳转到商品添加竞拍
			
		}else{
			Log::write("通过商品竞拍列表中添加商品竞拍....",Log::DEBUG) ;
			
			parent::add() ; 
		}
	
	}
	
	
	
	
	/**
	 * 添加竞拍保存
	 * @see CommonAction::insert()
	 */
	public function insert(){
		//接收页面参数 
		$data = Init_GP(array('gid','sn','goodsname','costprice','goodsprice','starttime','endtime','startprice','addprice','addsecond','usebeat','recommend','type','pointc','stockprice'));

		$goods = D('Goods');   
		if (!empty($data['gid'])){   //验证商品是否存在
			$info = $goods->find($data['gid']);
			if (empty($info)){
				$this->error('商品不存在');
			}
		}else{
			$this->error('商品不存在');
		}
		
		$data['realprice'] = $data['startprice'];         //竞标起始金额
		$data['starttime'] = strtotime($data['starttime']);      //竞拍开始结束时间
		$data['endtime'] = strtotime($data['endtime']); 
		
		
		parent::insert($data);
	}
	
	
	
	
	/**
	 * 修改竞拍
	 * @see CommonAction::edit()
	 */
	public function edit(){
		$id = GetNum($_GET['id']);   //竞拍主键ID

		//Log::write("修改商品竞拍....:".$id,Log::DEBUG) ;
		
		$auction = D('Auction');
		$auctiondata = $auction->where("id = $id")->select();   //根据主键获取商品竞拍
		
		cookie('endtime',$auctiondata['endtime'],3600);  //存cookie
		
		$this->assign("data",$auctiondata[0]);
		$this->display();
	}
	
	
	
	/**
	 * 修改竞拍保存
	 * @see CommonAction::update()
	 */
	public function update(){
		
		$data = Init_GP(array('gid','sn','goodsname','costprice','goodsprice','starttime','endtime','startprice','realprice','addprice','addsecond','usebeat','recommend','robots','type','pointc','stockprice'));
		$id = GetNum($_POST['id']);  //主键
		
		$goods = D('Goods');
		if (!empty($data['gid'])){     //验证竞拍商品
			$info = $goods->find($data['gid']);
			if (empty($info)){
				$this->error('商品不存在');
			}
		}else{
			$this->error('商品不存在');
		}
		
		
		$auction = D('Auction');
		$auctiondata = $auction->find($id);   //商品竞标信息
		
		$data['realprice'] = $auctiondata['realprice'] - $auctiondata['startprice'] +  $data['startprice'];    //竞标金额-起始金额 + 新开始金额=== （竞标过程所用竞标金额+新的开始金额）

		$data['starttime'] = strtotime($data['starttime']);
		$endtime = strtotime($data['endtime']);
		
		$cendtime = cookie('endtime');
		
		if ($endtime != $cendtime){   //cookia中获取结束时间和当前设置结束时间不同，重新设置结束时间
			$data['endtime'] = $endtime;
		}
		
		$awhere['id'] = $id;  
		
		parent::update($data,$awhere);
	}
	
	
	
	
	
	/**
	 * 查看商品
	 * Enter description here ...
	 */
	public function viewGoods(){
		$data = Init_GP(array('goodsname'));  //检索参数
		
		Log::write("选择商品......:", Log::DEBUG);
		
		$goods = D('Goods');
		$prefix = C('DB_PREFIX');   //配置参数中获取数据表前缀
			
		$goodsdata = $goods->Table("{$prefix}goods as G")->
							join("{$prefix}category as C ON G.cid = C.id")->
							field('G.*,C.categoryname')->
							where($goodsmap)->
							order('id desc')->findAll();
		
		$this->assign("goodsdata",$goodsdata);
		
		$this->display();
		
		Log::write("选择商品完毕......:" , Log::DEBUG);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 选择机器人
	 * Enter description here ...
	 */
	public function viewRobots(){
		$id = GetNum($_GET['id']);  //商品竞拍主键
		
		$user = D('User');
		$map['robot'] = array('eq',1);
		$data =	$user->where($map)->order('id desc')->findAll(); //选择机器人
		
		$robot = M('Robot');
		$robotmap['aid'] = array('eq',$id);
		$robotdata = $robot->where($robotmap)->findAll();  //商品竞标已经选择的机器人
		foreach ($robotdata as $val){
			$uids[] = $val['uid'];
		}
		
		$auction = D('Auction');   //商品竞拍
		$a = $auction->find($id);
		
		$this->assign("a",$a);
		$this->assign("uids",$uids);
		$this->assign("data",$data);
		
		$this->display();
	}
	
	
	
	
	
	
	/**
	 * 商品竞拍机器人设置保存
	 * Enter description here ...
	 */
	public function saveRobots(){
		$data = Init_GP(array('ids','aid','min','max','target'));   //接收竞拍机器人设置
		
		$auction = D('Auction');
		$count = count($data['ids']);  //机器人个数
		
		if($count >=3 ){  //商品竞拍机器人个数
			$auctiondata = array(
				'id'=>GetNum($data['aid']),   //竞拍ID
				'min'=>GetNum($data['min']),  //最小值
				'max'=>GetNum($data['max']),   //最大值
				'target'=>GetNum($data['target']), //目标金额
				'robots'=>1,                      //机器人
			);
			
			$auction->save($auctiondata);  //保存商品机器人设置
			
			
			$aid = GetNum($data['aid']);
			if (!empty($data)) {
				$robot = M('Robot');   //商品竞拍机器人表
				$map['aid'] = array('eq',$aid);
				$robot->where($map)->delete();   //删除商品竞拍机器人设置
				
				$robotdata['aid'] = $aid;
				foreach ($data['ids'] as $val){
					$robotdata['uid'] = $val;   //机器人用户ID
					$robot->add($robotdata);  //保存商品竞拍机器人设置
					
					unset($robotdata['uid']);				
				}
			}
			
		}else{   //少于3个机器人
			$auctiondata = array(
				'id'=>GetNum($data['aid']),
				'robots'=>0,
			);
			$auction->save($auctiondata);
		}
	}

	
	
	
	
	
	
	
	
	
	
	
	
	public function auction_end_show(){
		$id = GetNum($_GET['id']);

		$this->assign('id',$id);
		$this->display();
	}

	
	public function auction_end_save(){
		$data = Init_GP(array('id','uid'));
		$id = GetNum($data['id']);
		$uid = GetNum($data['uid']);
		$user = D('User');
		$userdata = $user->find($uid);
		
		if(empty($userdata)){
			$json = 0;//用户不存在
		}else{
			$auction = D('Auction');
			$public = A('Public');
			$cache = $public->_newCache();
			$auctiondata['uid'] = $userdata['id'];
			$auctiondata['auctionuname'] = $userdata['name'];
			$auctiondata['endtime'] = time();
			$auctiondata['state'] = 2;

			$map['id'] = array('eq',$id);
			$info = $auction->where($map)->save($auctiondata);
			
			$cache->clear();
			
			if($info){
				$json= 1;//成功
			}else{
				$json= 2;//失败
			}
		}
		echo $json;
	}
	
	
	
	
	
	
}
?>